package com.example.desinger.controller;

import com.example.desinger.common.R;
import com.example.desinger.domain.dto.PageDto;
import com.example.desinger.domain.query.PageQuery;
import com.example.desinger.entity.Students;
import com.example.desinger.service.StudentsService;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@Slf4j
@RequestMapping("/user")
@RestController
public class StudentsController {
    @Autowired
    private StudentsService studentsService;

    @PostMapping("/create")
    public R<String> registerByStudent(@RequestBody Students students) {
        String account = students.getAccount();
        int classId = students.getClassId();
        System.out.println("教室的id为" + classId);
        int i = studentsService.selectByAccount(account, classId);
        if (i == 1) {
            return R.error("用户已经注册，请重新填写！");
        }
        if (i == 0) {
            //用户没有注册
            //开始添加用户
            boolean save = studentsService.save(students);
            if (save) {
                return R.success("用户添加成功！");
            }
        }
        return R.error("数据库错误！！");
    }

    @PostMapping("/login")
    public R<Students> loginByPAndW(@RequestBody Students students) {
        return studentsService.selectStudentByPAndW(students);
    }


    @PostMapping("/findById")
    public R<PageDto<Students>> selectByPage(@RequestBody PageQuery pageQuery) {
        return studentsService.queryStudentsPage(pageQuery.getPage(),
                pageQuery.getLimit(), pageQuery.getClass_id());
    }

    // 班级学生分组
    @GetMapping("/group/{type}/{class_id}")
    public R<ArrayList> opertorGroup(@PathVariable("type") int type, @PathVariable("class_id") int class_id) {
        ArrayList groups = studentsService.opertorGroup(type, class_id);

//        return studentsService.queryStudentsPage(pageQuery.getPage(),
//                pageQuery.getLimit(),pageQuery.getClass_id());

        return R.success(groups, "分组成功");
    }

    @PostMapping("/addStudents")
    public R<String> addStudents(MultipartFile file, int classId) {

        return studentsService.addStudents(file, classId);

    }

}
